//
//  YSLTabBarController.m
//  TabBar
//
//  Created by lanou on 16/7/11.
//  Copyright © 2016年 杨斯霖. All rights reserved.
//

#import "YSLTabBarController.h"

@interface YSLTabBarController ()<UITabBarControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic, strong)UIImagePickerController *imagePicker;

@end

@implementation YSLTabBarController



//当第一次使用这个类的时候会调用一次
+ (void)initialize {
    
#pragma mark appearance统一设置 (UI_APPEARANCE_SELECTOR)
    //通过appearance统一设置所有UITabBarItem的文字属性
    //后面带有UI_APPEARANCE_SELECTOR的方法,都可以通过appearance对象来统一设置
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    //添加子控制器

    [self setupChildVC:[YSLHomeViewController new] title:@"首页" image:@"zhuye" selectedImage:@"3"];
    
    [self setupChildVC:[KLBuyViewController new] title:@"分类" image:@"fenlei" selectedImage:@"6"];
    
    [self setupChildVC:[ChatViewController new] title:@"消息" image:@"message_normal" selectedImage:@"message_highlight"];
    
    [self setupChildVC:[[KLMainViewController alloc] init] title:@"我的" image:@"wode" selectedImage:@"5"];
    
    
    //更换自定义tabBar
    [self setValue:[[YSLTabBar alloc] init] forKey:@"tabBar"];
    
}

#pragma mark - 相机
//发布 调用系统相机 保存照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
   
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage * image=[info objectForKey:UIImagePickerControllerEditedImage];
    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    
//    [self performSelector:@selector(selectPic) withObject:image afterDelay:0.1];
    
    //设置调用本地相册
    self.imagePicker = [[UIImagePickerController alloc]init];
    self.imagePicker.delegate = self;
    
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    //模态出本地相册
    YSLPicViewController * picVC = [[YSLPicViewController alloc]init];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController: [[UINavigationController alloc] initWithRootViewController:picVC] animated:YES completion:nil];
    
    
}

//- (void)selectPic
//
//{
////    //模态出本地相册
//    YSLPicViewController * picVC = [[YSLPicViewController alloc]init];
//    [self presentViewController:picVC animated:YES completion:nil];
////    [self.navigationController pushViewController:picVC animated:YES];
//
//}


#pragma mark 封装子控制器
//初始化子控制器
- (void)setupChildVC:(UIViewController *)VC
               title:(NSString *)title
               image:(NSString *)image
       selectedImage:(NSString *)selectedImage
{

    //设置文字和图片
    VC.tabBarItem.title = title;
    VC.tabBarItem.image = [UIImage imageNamed:image];
    VC.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
        VC.navigationItem.title = title;

    //包装一个导航控制器,添加导航控制器为TabBarController的子控制器
    YSLNavigationController *nav = [[YSLNavigationController alloc] initWithRootViewController:VC];
    [self addChildViewController:nav];
}

#pragma tabBar 选择点击事件
//- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
//{
//    
//    if (self.selectedIndex == 2 ) {
//      

//        
//    }
//    NSLog(@"%lu",(unsigned long)self.selectedIndex);
//}

//
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
//    UINavigationController *nvc = (UINavigationController *)viewController;
//
//    if ([[nvc.childViewControllers[0] class] isSubclassOfClass:[YSLMeViewController class]] || [[nvc.childViewControllers[0] class] isSubclassOfClass:[YSLMessageViewController class]]) {
//        YSLLoginRegisterViewController *login = [YSLLoginRegisterViewController new];
//        [self presentViewController:login animated:YES completion:nil];
//        return NO;
//    }
    
    return YES;
}
@end
